package com.weixin.paleituo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.util.UIUtils;

/**
 * Recycler分割线
 *
 * @author fengzhen
 * @version v1.0, 2018/4/23
 */
public class GridleDecoration extends RecyclerView.ItemDecoration {
    /* 分割线 */
    private final Drawable mDrawable;
    private int mHeight;
    private int mWidth;
    private boolean mHasHead = true;

    /**
     * 构造方法
     */
    public GridleDecoration(Context context) {
        mDrawable = ContextCompat.getDrawable(context, R.drawable.decoration);
        mHeight = UIUtils.dip2px(20);
        mWidth = UIUtils.dip2px(10);
        this.mHasHead = true;
    }

    public GridleDecoration(Context context,boolean hasHead) {
        mDrawable = ContextCompat.getDrawable(context, R.drawable.decoration);
        mHeight = UIUtils.dip2px(20);
        mWidth = UIUtils.dip2px(10);
        this.mHasHead = hasHead;
    }

    /**
     * 获取列数 或 行数
     */
    private int getSpanCount(RecyclerView parent) {
        int spanCount = 0;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }
        return spanCount;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//        // 画水平分割线
//        drawHorizontal(c, parent, state);
//        // 画垂直分割线
//        drawVertical(c, parent, state);
    }

    /**
     * 水平分割线
     */
    private void drawHorizontal(Canvas c, RecyclerView parent, RecyclerView.State state) {
    }

    /**
     * 垂直分割线
     */
    private void drawVertical(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int childCount = parent.getChildCount();
        View childAt;
        RecyclerView.LayoutParams layoutParams;
        int left, right, top, bottom;

        if (mHasHead) {
            childAt = parent.getChildAt(0);
            layoutParams = (RecyclerView.LayoutParams) childAt.getLayoutParams();

            left = childAt.getRight() + layoutParams.rightMargin;
            top = childAt.getTop() + layoutParams.topMargin;
            right = left + mHeight;
            bottom = childAt.getBottom() - layoutParams.bottomMargin;

            mDrawable.setBounds(left, top, right, bottom);
            mDrawable.draw(c);

            mDrawable.setBounds(0, top, mHeight, bottom);
            mDrawable.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        if (mHasHead) {
            if (itemPosition == 0) {
                outRect.set(0, 0, 0, 0);
            } else if (itemPosition % 2 == 1) {
                // 右侧
                outRect.set(mHeight, mWidth, mWidth, 0);
            } else {
                outRect.set(mWidth, mWidth, mHeight, 0);
            }
        } else {
            if (itemPosition % 2 == 1) {
                outRect.set(mWidth, mWidth, mHeight, 0);
            } else {
                outRect.set(mHeight, mWidth, mWidth, 0);
            }
        }
    }
}
